package com.moorror.codegenvel.utils;

import com.moorror.codegenvel.view.window.codegen.domain.ColumnDesc;
import com.moorror.codegenvel.view.window.codegen.domain.TableConstant;
import com.moorror.codegenvel.view.window.codegen.domain.TableDesc;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;


/**
 * @Author jiyanghuang
 * @Date 2025/8/28 00:51
 */
public class SQLUtils {

    public static TableDesc parser(String sql, int fillPrefixLen) {
        try {
            Statement statement = CCJSqlParserUtil.parse(sql);
            if (statement instanceof CreateTable) {
                TableDesc tableDesc = new TableDesc();
                CreateTable createTable = (CreateTable) statement;
                String tableName = clearStartEnd(createTable.getTable().getName());
                String[] names = tableName.split("_");
                // TODO 名称还得约定一下， 表注释
                tableDesc.setNameTb(tableName);
                tableDesc.setName(NameUtils.className(tableName));
                tableDesc.setComment(commentTable(createTable));
                if(names.length <= fillPrefixLen){
                    return null;
                }
                if (fillPrefixLen == 0) {
                    tableDesc.setNameReal(NameUtils.className(tableName));
                    tableDesc.setPreName("");
                } else if (fillPrefixLen > 0) {
                    int pos = 0;
                    for (int i = 0; i < fillPrefixLen; i++) {
                        pos += names[i].length() + 1;
                    }
                    tableDesc.setNameReal(NameUtils.className(tableName.substring(pos)));
                    tableDesc.setPreName(NameUtils.firstLowerCase(NameUtils.className(tableName.substring(0, pos - 1))));
                }
                tableDesc.setNameRealLow(StringUtils.uncapitalize(tableDesc.getNameReal()));
                if (CollectionUtils.isNotEmpty(createTable.getColumnDefinitions())) {
                    for (ColumnDefinition columnDefinition : createTable.getColumnDefinitions()) {
                        ColumnDesc columnDesc = new ColumnDesc();
                        columnDesc.setColumnName(clearStartEnd(columnDefinition.getColumnName()));
                        columnDesc.setName(NameUtils.javaName(clearStartEnd(columnDefinition.getColumnName())));
                        columnDesc.setType(clearType(columnDefinition.getColDataType().getDataType()));
                        tableDesc.addColumn(columnDesc);
                        if (CollectionUtils.isNotEmpty(columnDefinition.getColumnSpecs())) {
                            for (int i = 0; i < columnDefinition.getColumnSpecs().size(); i++) {
                                if (columnDefinition.getColumnSpecs().get(i).equals("COMMENT") && (i + 1) < columnDefinition.getColumnSpecs().size()) {
                                    columnDesc.setComment(clearStartEnd(columnDefinition.getColumnSpecs().get(i + 1)));
                                }
                            }
                        }
                    }
                }
                return tableDesc;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String clearStartEnd(String name) {
        if (name.startsWith("'") || name.startsWith("`")) {
            name = name.substring(1, name.length());
        }
        if (name.endsWith("'") || name.endsWith("`")) {
            name = name.substring(0, name.length() - 1);
        }
        return name;
    }

    public static String clearType(String type) {
        int pos = type.indexOf("(");
        if (pos > 0) {
            type = type.substring(0, pos);
        }
        type = type.trim();
        return TableConstant.fetchType(type);
    }

    public static String commentTable(CreateTable createTable) {
        List<String> values = createTable.getTableOptionsStrings();
        for (int i = 0; i < values.size(); i++) {
            if ("COMMENT".equals(values.get(i))) {
                if ((i + 2) < values.size()) {
                    return values.get(i + 2);
                }
            }
        }
        return "";
    }
}
